package com.totoro.tymapping.manager;

import com.totoro.tymapping.TyMapping;
import com.totoro.tymapping.constants.StaticSystemVarConst;
import com.totoro.tymapping.pojo.Config;
import com.totoro.tymapping.utils.ColorUtil;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Totoro
 * @version 0.1.0
 * @date 2025/8/13 - 2:35
 * @description 配置管理器
 **/
public class ConfigManager {
    private final TyMapping plugin;
    private Config config;

    public ConfigManager(TyMapping plugin) {
        this.plugin = plugin;
        this.config = new Config();
        reloadConfig();
    }

    /**
     * @author totoro
     * @description 初始化插件文件
     * @date  2:37
     * @return void
     **/
    private void initConfigFile() {
        if (!this.plugin.getDataFolder().exists()){
            this.plugin.getDataFolder().mkdirs();
            this.plugin.saveDefaultConfig();
        }
    }

    /**
     * @author totoro
     * @description 重载配置
     * @date  2:43
     * @return void
     **/
    public void reloadConfig() {
        initConfigFile();
        plugin.reloadConfig();
        FileConfiguration configuration = plugin.getConfig();
        boolean attributeValueRound = configuration.getBoolean("attribute-value-round", false);
        List<String> readSlot = configuration.getStringList("read-slot");
        Map<String, Config.Check> checks = new LinkedHashMap<>();
        if (configuration.isConfigurationSection("checks")) {
            ConfigurationSection checksSection = configuration.getConfigurationSection("checks");
            Set<String> checkKeys = checksSection.getKeys(false);
            for (String checkKey : checkKeys) {
                if (checksSection.isConfigurationSection(checkKey)) {
                    ConfigurationSection checkSection = checksSection.getConfigurationSection(checkKey);
                    List<String> condition = checkSection.getStringList("condition");
                    if(null == condition) {
                        condition = StaticSystemVarConst.EMPTY_LIST;
                    } else {
                        condition = condition.stream().map(ColorUtil::color).collect(Collectors.toList());
                    }
                    List<String> attribute = checkSection.getStringList("attribute");
                    if(null == attribute) {
                        attribute = StaticSystemVarConst.EMPTY_LIST;
                    } else {
                        attribute = attribute.stream().map(ColorUtil::color).collect(Collectors.toList());
                    }
                    List<String> effect = checkSection.getStringList("effect");
                    if(null == effect) {
                        effect = StaticSystemVarConst.EMPTY_LIST;
                    }
                    checks.put(checkKey, config.new Check(condition, attribute, effect));
                    plugin.getServer().getConsoleSender().sendMessage(StaticSystemVarConst.CONSOLE_PREFIX +
                    String.format(ColorUtil.color("&f映射检测组: &e[%s] &f已载入！"), checkKey));
                }
            }
        }
        this.config = new Config(attributeValueRound, readSlot, checks);
    }

    public Config getConfig() {
        return config;
    }
}
